package com.aihna.common.pay.service.impl;

import com.aihna.common.core.utils.MapstructUtils;
import com.aihna.common.mybatis.core.page.TableDataInfo;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.aihna.common.pay.domain.SysPaymentApp;
import com.aihna.common.pay.domain.bo.SysPaymentAppBo;
import com.aihna.common.pay.domain.vo.SysPaymentAppVo;
import com.aihna.common.pay.mapper.SysPaymentAppMapper;
import com.aihna.common.pay.service.ISysPaymentAppService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.aihna.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 支付应用Service业务层处理
 *
 * @author Joseph.Chen
 * @date 2024-03-09
 */
@RequiredArgsConstructor
@Service
public class SysPaymentAppServiceImpl implements ISysPaymentAppService {

    private final SysPaymentAppMapper baseMapper;

    /**
     * 查询支付应用
     */
    @Override
    public SysPaymentAppVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询支付应用列表
     */
    @Override
    public TableDataInfo<SysPaymentAppVo> queryPageList(SysPaymentAppBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPaymentApp> lqw = buildQueryWrapper(bo);
        Page<SysPaymentAppVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询支付应用列表
     */
    @Override
    public List<SysPaymentAppVo> queryList(SysPaymentAppBo bo) {
        LambdaQueryWrapper<SysPaymentApp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysPaymentApp> buildQueryWrapper(SysPaymentAppBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPaymentApp> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysPaymentApp::getName, bo.getName());
        lqw.eq(bo.getStatus() != null, SysPaymentApp::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增支付应用
     */
    @Override
    public Boolean insertByBo(SysPaymentAppBo bo) {
        SysPaymentApp add = MapstructUtils.convert(bo, SysPaymentApp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改支付应用
     */
    @Override
    public Boolean updateByBo(SysPaymentAppBo bo) {
        SysPaymentApp update = MapstructUtils.convert(bo, SysPaymentApp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysPaymentApp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除支付应用
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
